<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataStore: async () =>
            Vue.extend(
                await Vue.prototype.$vFN.Components.Base.SelectDataStore()
            ),
  },
  data() {
    return {
      appWindow: window,

      // 表单
      form: {
        store_id: [],
        name: "",
        cost_ratio: "",
        type: [],
        status: 1,
        pay_type: [],
        integral: [],
      },

      // 页面接口地址
      apiUrl: {
        add:"plugin/cashier-desk/admin/setting/special-add",
        edit: "plugin/cashier-desk/admin/setting/edit",
        amendEdit: "plugin/cashier-desk/admin/setting/special-edit",
      },

      // 选择抵扣列表
      integralList: [],
      // 交易方式列表
      payTypeList: [],

      load: true,
      // 关闭弹窗
      editVisible: false,
      // 选择的门店列表
      storeList: [],
    };
  },
  methods: {

    // 确定添加
    async add() {

    if (!(await this.validate())) {
         return;
      }

      let res = await this.$vFN.Http.post({
        url: this.apiUrl.add,
        data: {
          cost_ratio: this.form.cost_ratio,
          id: this.form.store_id.toString(),
        }
      },window);
      if(res.code == 200) {
        this.editVisible = false;
        this.$vFN.Tools.messageSuccess("添加成功");
        this.onSuccess();
      }
    },

    // 编辑设置确定按钮
    async sum() {

     // 平台提成不能为空
     if(this.$vFN.Utils.isEmpty(this.form.cost_ratio)){
       this.$vFN.Tools.messageError("平台提成不能为空");
       return;
     }
      
      let res = await this.$vFN.Http.post({
        url: this.apiUrl.edit,
        data: {
          status:     this.form.status,
          cost_ratio: this.form.cost_ratio,
          integral:   this.form.integral,
          pay_type:   this.form.pay_type,

        }
      }, window);

      if(res.code == 200) {
        this.$vFN.Tools.messageSuccess("保存成功");
        this.onSuccess();
        this.editVisible = false;
      }
    },  

    // 修改门店平台提成修改按钮
   async amend(){
    
     // 平台提成不能为空
     if(this.$vFN.Utils.isEmpty(this.form.cost_ratio)){
       this.$vFN.Tools.messageError("平台提成不能为空");
       return;
     }

    let res = await this.$vFN.Http.post({
        url: this.apiUrl.amendEdit,
        data:{
              id: this.form_data.id,
              cost_ratio: this.form.cost_ratio,
             },
      }, window);

      if(res.code == 200) {
        this.$vFN.Tools.messageSuccess("修改成功");
        this.onSuccess();
        this.editVisible = false;
      }

    },

    // 验证
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
          valid ? resolve(true) : resolve(false);
        });
      });
    },

     // 验证平台提成数值
    validatorNumber(rule, value, callback){

          // 0-100包含小数,限制两位
          if( parseFloat(value) > 100 ) {
             callback(new Error("最大100%"));
          }else{
               this.$vFN.Tools.validateMoney(rule, value, callback);
          }
    },

    // 初始化
    async init() {
      this.load = true;

      this.editVisible   = true;

      // 编辑设置   
      if(this.editType == 'basic'){
         let {cost_ratio,integral_list,pay_type_list,status,type } = this.form_data;

          this.form.cost_ratio =  cost_ratio;
          this.form.integral   = integral_list;
          this.form.pay_type   = pay_type_list;
          this.form.status     = status;
          this.form.type       = type;
          this.integralList    = this.deductList;
          this.payTypeList     = this.paymentList;
      }
   
      // 修改平台提成
     if(this.editType == 'update'){
        let {cost_ratio}         = this.form_data;
        this.form.cost_ratio     = cost_ratio;
       }
   
      this.load = false;
    },
    // 成功回调
    onSuccess() {},
    // 取消回调
    onCancel() {},
    // 失败回调
    onError() {},
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>